package com.yongcheon.sms_manager.ui.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.activities.SpamSentenceActivity;
import com.yongcheon.sms_manager.ui.preference.SpamSentenceLongClickListener;
import com.yongcheon.sms_manager.vo.SpamSentenceVO;

/**
 * Created by yongcheon on 2014-12-12.
 */
public class SpamSenetnceRegistDlg {

    public void showRegSentenceDlg(final Context context, View view, SpamSentenceVO spamentenceVO, Object callObj) {
        if(spamentenceVO == null) spamentenceVO = new SpamSentenceVO();
        LinearLayout linear = (LinearLayout) view.inflate(context, R.layout.spam_sentence_regist, null);
        if(spamentenceVO.getSentence() != null && !spamentenceVO.getSentence().equals("")){
            EditText ed_sentence = (EditText) linear.findViewById(R.id.spam_sentence);
            ed_sentence.setText(spamentenceVO.getSentence());
        }

        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(context.getResources().getString(R.string.regist_spam_sentence))
                .setView(linear)
                .setPositiveButton(context.getResources().getString(R.string.confirm), null)
                .setNegativeButton("취소", null)
                . create();

        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

        Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        theButton.setOnClickListener(new CustomListener(dialog, context, spamentenceVO, callObj));
    }

    class CustomListener implements View.OnClickListener {
        private Dialog dialog;
        private Context context;
        private SpamSentenceVO spamentenceVO;
        private Object callObj;


        public CustomListener(Dialog dialog, Context context, SpamSentenceVO spamentenceVO, Object callObj) {
            this.dialog = dialog;
            this.context = context;
            this.spamentenceVO = spamentenceVO;
            this.callObj = callObj;
        }

        @Override
        public void onClick(View v) {
            EditText ed_sentence = (EditText) dialog.findViewById(R.id.spam_sentence);
            String sentence = ed_sentence.getText().toString().trim();
            if(sentence == null || sentence.equals("")){
                Toast.makeText(context, context.getResources().getString(R.string.not_input_spam_sentence), Toast.LENGTH_SHORT).show();
            }else{
                spamentenceVO.setSentence(sentence);
                if(callObj instanceof SpamSentenceActivity){
                    SpamSentenceActivity spamSentenceActivity = (SpamSentenceActivity)callObj;
                    spamSentenceActivity.addSentence(spamentenceVO);
                }else if(callObj instanceof SpamSentenceLongClickListener.SpamDialogOnClickListener){
                    SpamSentenceLongClickListener.SpamDialogOnClickListener listener = (SpamSentenceLongClickListener.SpamDialogOnClickListener)callObj;
                    listener.updateSentence(spamentenceVO);
                }
                dialog.dismiss();
            }
        }
    }
}
